package cz.data.domain.visual.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.DataConstant;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.visual.mapstruct.DataSetMapper;
import cz.data.domain.visual.model.dto.DataSetDto;
import cz.data.domain.visual.model.dto.SqlParseDto;
import cz.data.domain.visual.model.entity.DataSetEntity;
import cz.data.domain.visual.model.query.DataSetQuery;
import cz.data.domain.visual.model.vo.DataSetVo;
import cz.data.domain.visual.service.DataSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"数据集"})
@RestController
@RequestMapping("/dataSets")
public class DataSetController extends BaseController {

    @Autowired
    private DataSetService dataSetService;

    @Autowired
    private DataSetMapper dataSetMapper;

    @ApiOperation(value = "数据集-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<DataSetVo> getVisualDataSetById(@PathVariable String id) {
        DataSetEntity dataSetEntity = dataSetService.getDataSetById(id);
        return new SimpleDataResponse<>(dataSetMapper.toVO(dataSetEntity));
    }

    @ApiOperation(value = "数据集-获取列表")
    @GetMapping("/list")
    public TableDataResponse<DataSetVo> getVisualDataSetList() {
        QueryWrapper<DataSetEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", DataConstant.EnableState.ENABLE.getKey());
        queryWrapper.select("id", "set_name");
        List<DataSetEntity> list = dataSetService.list(queryWrapper);
        List<DataSetVo> collect = list.stream().map(dataSetMapper::toVO).collect(Collectors.toList());
        return new TableDataResponse<>(collect);
    }

    @ApiOperation(value = "数据集-分页查询")
    @ApiImplicitParam(name = "visualDataSetQuery", value = "查询实体visualDataSetQuery", required = true, dataTypeClass = DataSetQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<DataSetVo>> getVisualDataSetPage(DataSetQuery dataSetQuery) {
        QueryWrapper<DataSetEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dataSetQuery.getSetName()), "set_name", dataSetQuery.getSetName());
        IPage<DataSetEntity> page = dataSetService.page(new Page<>(dataSetQuery.getPageNum(), dataSetQuery.getPageSize()), queryWrapper);
        List<DataSetVo> collect = page.getRecords().stream().map(dataSetMapper::toVO).collect(Collectors.toList());
        JsonPage<DataSetVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "数据集-新增")
    @ApiImplicitParam(name = "dataSet", value = "详细实体dataSet", required = true, dataType = "DataSetDto")
    @PostMapping()
    public SimpleDataResponse<DataSetVo> saveVisualDataSet(@RequestBody @Validated({ValidationGroups.Insert.class}) DataSetDto dataSet) {
        DataSetEntity dataSetEntity = dataSetService.saveDataSet(dataSet);
        return new SimpleDataResponse<>(dataSetMapper.toVO(dataSetEntity));
    }

    @ApiOperation(value = "数据集-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dataSet", value = "详细实体dataSet", required = true, dataType = "DataSetDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<DataSetVo> updateVisualDataSet(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) DataSetDto dataSet) {
        DataSetEntity dataSetEntity = dataSetService.updateDataSet(dataSet);
        return new SimpleDataResponse<>(dataSetMapper.toVO(dataSetEntity));
    }

    @ApiOperation(value = "数据集-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteVisualDataSetById(@PathVariable String id) {
        dataSetService.deleteDataSetById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据集-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteVisualDataSetBatch(@PathVariable List<String> ids) {
        dataSetService.deleteDataSetBatch(ids);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据集-SQL解析")
    @ApiImplicitParam(name = "sqlParseDto", value = "SQL解析实体sqlParseDto", required = true, dataType = "SqlParseDto")
    @PostMapping("/sql/analyse")
    public TableDataResponse<String> sqlAnalyse(@RequestBody @Validated SqlParseDto sqlParseDto) {
        List<String> list = dataSetService.sqlAnalyse(sqlParseDto);
        return new TableDataResponse<>(list);
    }
}
